package com.mobileares.midp.widgets.client.list;

import com.google.gwt.event.logical.shared.HasSelectionHandlers;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.mobileares.midp.widgets.client.utils.IWidgetProvider;

import java.util.Iterator;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2011-4-8
 * Time: 10:14:10
 * To change this template use File | Settings | File Templates.
 */
public class ListWidget extends Composite implements HasSelectionHandlers<ListItem> {
//    private FocusPanel wrapper;
    protected FlowPanel container;
    private IWidgetProvider provider;
    private ListItem selected;

    public ListWidget() {
        container = new FlowPanel();
        container.setStyleName("list-wrapper");
//        wrapper = new FocusPanel(container);
//        wrapper.setStyleName("list-wrapper");
        initWidget(container);
        init();
    }

//    public void setFocus(boolean enable) {
//        wrapper.setFocus(enable);
//    }


    public IWidgetProvider getProvider() {
        return provider;
    }

    private void init() {
//        wrapper.addKeyDownHandler(new KeyDownHandler() {
//            public void onKeyDown(KeyDownEvent event) {
//                if (event.getNativeKeyCode() == KeyCodes.KEY_UP) {
//                    fireSelected(true);
//                }
//                if (event.getNativeKeyCode() == KeyCodes.KEY_DOWN) {
//                    fireSelected(false);
//                }
//            }
//        });
    }

//    private void fireSelected(boolean up) {
//        int index = 0;
//        if (selected == null) {
//            if (up) return;
//
//        } else {
//            index = container.getWidgetIndex(selected);
//            if (up) index--;
//            else
//                index++;
//            if (index < 0) index = 0;
//            if (index == container.getWidgetCount()) index--;
//        }
//        ((ListItem) container.getWidget(index)).clickHandler.onClick(null);
//
//    }

    public void setProvider(IWidgetProvider provider) {
        this.provider = provider;
    }

    public void addItem(ListItem item) {
        container.add(item);
        item.setParent(this);
    }

    public void addItem(Object ob) {
        ListItem item = new ListItem(ob);
        addItem(item);
    }

    /**
     * Adds a {@link com.google.gwt.event.logical.shared.SelectionEvent} handler.
     *
     * @param listItemSelectionHandler the handler
     * @return the registration for the event
     */
    public HandlerRegistration addSelectionHandler(SelectionHandler<ListItem> listItemSelectionHandler) {
        return addHandler(listItemSelectionHandler, SelectionEvent.getType());

    }

    public int getItemIndex(ListItem item) {
        return container.getWidgetIndex(item);
    }

    public void insertItem(int index, ListItem item) {
        insertItem(index, item, false);
    }

    public void insertItem(int index, ListItem item, boolean newItem) {
        container.insert(item, index);
        if (newItem)
            item.setParent(this);
    }

    public ListItem getSelected() {
        return selected;
    }

    public int getWidgetCount() {
        return container.getWidgetCount();
    }

    public ListItem getItem(Object userObject) {
        if (userObject == null) return null;
        for (int i = 0; i < container.getWidgetCount(); i++) {
            ListItem item = (ListItem) container.getWidget(i);
            if (item.getUserObject().equals(userObject))
                return item;
        }
        return null;
    }

    public ListItem getItemByCode(Object code) {
        if (code == null) return null;
        for (int i = 0; i < container.getWidgetCount(); i++) {
            ListItem item = (ListItem) container.getWidget(i);
            if (provider.getUserObject(item.getUserObject()).equals(code))
                return item;
        }
        return null;
    }

    void setSelected(ListItem selected, boolean fireSelectionEvent) {
        for (int i = 0; i < container.getWidgetCount(); i++) {
            ListItem item = (ListItem) container.getWidget(i);
            if (selected != null && item == selected) {
//            if (selected != null && item.getUserObject().equals(selected.getUserObject())) {
                if (this.selected != null) this.selected.removeStyleDependentName("selected");
                this.selected = item;
                this.selected.addStyleDependentName("selected");
                if (fireSelectionEvent)
                    SelectionEvent.fire(this, item);
                return;
            }

        }
        if (this.selected != null)
            this.selected.removeStyleDependentName("selected");
        this.selected = null;
    }

    public void setSelected(Object ob, boolean fireSelectionEvent) {
        for (int i = 0; i < container.getWidgetCount(); i++) {
            ListItem item = (ListItem) container.getWidget(i);
            if (ob != null && provider.getUserObject(item.getUserObject()).equals(ob)) {
                setSelected(item, fireSelectionEvent);
                return;
            }

        }
        if (this.selected != null)
            this.selected.removeStyleDependentName("selected");
        this.selected = null;
    }

    public void setHeight(String height) {
        super.setHeight(height);
        DOM.setStyleAttribute(getElement(), "overflow", "auto");
    }

    public void removeAll() {
        container.clear();
        selected = null;
    }

    public Iterator getIterator() {
        return container.iterator();
    }

}
